﻿// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/shuttlePanel/shuttlePanel.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
            checkNextBus();
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });
})();

var monTothuLoyolaArray = new Array("07:45", "08:00", "08:15", "08:45", "09:00", "09:15", "09:45", "10:00", "10:15", "10:45", "11:00", "11:20", "11:40", "12:00", "12:20", "12:40", "13:00", "13:20", "13:40", "14:00", "14:20", "14:40", "15:00", "15:20", "15:40", "16:00", "16:20", "16:40", "17:00", "17:20", "17:40", "18:00", "18:30", "18:45", "19:30", "20:00", "20:30", "20:50", "21:20", "21:40", "22:10", "22:30", "23:00");
var fridayLoyolaArray = new Array("07:45", "08:15", "08:45", "09:15", "09:30", "10:05", "10:15", "10:30", "11:00", "11:15", "11:30", "12:00", "12:15", "12:30", "13:00", "13:15", "13:30", "14:00", "14:25", "14:45", "15:15", "15:30", "16:00", "16:15", "16:45", "17:00", "17:15", "17:45", "18:15", "18:45", "19:15");
var monTothuSGWArray = new Array("07:45", "08:15", "08:30", "08:45", "09:15", "09:30", "09:45", "10:15", "10:30", "10:40", "11:05", "11:20", "11:40", "12:00", "12:20", "12:40", "13:00", "13:20", "13:40", "14:00", "14:20", "14:40", "15:00", "15:20", "15:40", "16:00", "16:20", "16:40", "17:00", "17:20", "17:40", "18:00", "18:15", "19:00", "19:35", "20:05", "20:25", "20:55", "21:15", "21:45", "22:05", "22:35", "23:00");
var fridaySGWArray = new Array("07:45", "08:15", "08:45", "09:15", "09:40", "09:55", "10:30", "10:45", "11:00", "11:30", "11:45", "12:00", "12:30", "12:45", "13:00", "13:30", "13:45", "14:00", "14:25", "14:55", "15:10", "15:45", "15:55", "16:30", "16:45", "17:15", "17:30", "17:45", "18:15", "18:45", "19:15");

function checkNextBus() {
    var d = new Date();
    var currenttime = d.getHours() + ":" + d.getMinutes();
    var currentDate = d.getDay();
    var timeSGW = document.getElementById('TimeSGW');
    var timeLoy = document.getElementById('TimeLayola');
    var found = false;

    if (currentDate == 0 || currentDate == 6) { // If it is either Saturday or Sunday, display no buses.
        timeSGW.innerHTML = "No buses";
        timeLoy.innerHTML = "No buses";
    } // End of week-end search
    else if (currentDate == 5) { // If its is Friday, then search the friday array.

        for (var i = 0; i < fridaySGWArray.length; i++) {
            if (fridaySGWArray[i] >= currenttime) {
                timeSGW.innerHTML = fridaySGWArray[i];
                found = true;
                break;
            }
        }

        if (!found)
            timeSGW.innerHTML = "No More Buses";

        found = false;

        for (var i = 0; i < fridayLoyolaArray.length; i++) {
            if (fridayLoyolaArray[i] >= currenttime) {
                timeLoy.innerHTML = fridayLoyolaArray[i];
                found = true;
                break;
            }
        }

        if (!found)
            timeLoy.innerHTML = "No More Buses";

    } // End of friday search
    else { // If it is from Monday to Thursday, then search the corresponding array

        for (var i = 0; i < monTothuSGWArray.length; i++) {
            if (monTothuSGWArray[i] >= currenttime) {
                timeSGW.innerHTML = monTothuSGWArray[i];
                found = true;
                break;
            }
        }

        if (!found)
            timeSGW.innerHTML = "No More Buses";

        found = false;

        for (var i = 0; i < monTothuLoyolaArray.length; i++) {
            if (monTothuLoyolaArray[i] >= currenttime) {
                timeLoy.innerHTML = monTothuLoyolaArray[i];
                found = true;
                break;
            }
        }

        if (!found)
            timeLoy.innerHTML = "No More Buses";
    } // End of week days search


}

function onSchOver() {
    var box = document.getElementById('fullSch');
    var title = document.getElementById('fullSchText');
    box.style.transition = "all 0.5s ease";
    box.style.boxShadow = "0 0 30px rgba(255,255,255, 0.9)";
    title.style.textShadow = "10px 10px 0px rgba(10,10,10,0.1)";
}

function onSchOut() {
    var box = document.getElementById('fullSch');
    var title = document.getElementById('fullSchText');
    box.style.transition = "all 0.5s ease";
    box.style.boxShadow = "0 0 10px rgba(255,255,255, 0.9)";
    title.style.transition = "all 0.5s ease";
    title.style.textShadow = "4px 4px 0px rgba(0,0,0,0.1)";
}

function showSch() {

    var div = document.getElementById('scheduleMFHidden');
    var sch = document.getElementById('fullSchText');
    var hour = document.getElementById('hours');
    var d = new Date();
    var currenttime = d.getHours() + ":" + d.getMinutes();

    if (d.getDay() != 5) { // If it is from Monday to Thursday

            if (document.getElementById('Mon').checked && d.getDay() == 1) { // If it is monday and monday is checked then show the corresponding schedule
                MSApp.execUnsafeLocalFunction(function () {
                    hour.innerHTML = "";
                    for (var i = 0; i < monTothuSGWArray.length; i++) {
                        if (currenttime <= monTothuSGWArray[i])
                            hour.innerHTML += "<input type='checkbox' onclick='createToast(\"" + monTothuSGWArray[i] + "\");'>At : " + monTothuSGWArray[i] + "</input><br/>";
                        else
                            continue;
                    }
                });
            }
            else if (document.getElementById('Tue').checked && d.getDay() == 2) { // If it is monday and monday is checked then show the corresponding schedule
                MSApp.execUnsafeLocalFunction(function () {
                    hour.innerHTML = "";
                    for (var i = 0; i < monTothuSGWArray.length; i++) {
                        if (currenttime <= monTothuSGWArray[i])
                            hour.innerHTML += "<input type='checkbox' onclick='createToast(\"" + monTothuSGWArray[i] + "\");'>At : " + monTothuSGWArray[i] + "</input><br/>";
                        else
                            continue;
                    }
                });
            }
            else if (document.getElementById('Wed').checked && d.getDay() == 3) { // If it is monday and monday is checked then show the corresponding schedule
                MSApp.execUnsafeLocalFunction(function () {
                    hour.innerHTML = "";
                    for (var i = 0; i < monTothuSGWArray.length; i++) {
                        if (currenttime <= monTothuSGWArray[i])
                            hour.innerHTML += "<input type='checkbox' onclick='createToast(\"" + monTothuSGWArray[i] + "\");'>At :" + monTothuSGWArray[i] + "</input><br/>";
                        else
                            continue;
                    }
                });
            } 
            else if (document.getElementById('Thu').checked && d.getDay() == 4) { // If it is monday and monday is checked then show the corresponding schedule
                MSApp.execUnsafeLocalFunction(function () {
                    hour.innerHTML = "";
                    for (var i = 0; i < monTothuSGWArray.length; i++) {
                        if (currenttime <= monTothuSGWArray[i])
                            hour.innerHTML += "<input type='checkbox' onclick='createToast(\"" + monTothuSGWArray[i] + "\");'>At :" + monTothuSGWArray[i] + "</input><br/>";
                        else
                            continue;
                    }
                });
            }
            else { // Else show the whole week schedule
                MSApp.execUnsafeLocalFunction(function () {
                    hour.innerHTML = "";
                    for (var i = 0; i < monTothuSGWArray.length; i++)
                        hour.innerHTML += "At : " + monTothuSGWArray[i] + "<br/>";
                });
            }
    }

    else { // Else that means it's friday
        if (document.getElementById('Fri').checked && d.getDay() == 5)
            MSApp.execUnsafeLocalFunction(function () {
                hour.innerHTML = "";
                for (var i = 0; i < fridaySGWArray.length; i++) {
                    if (currenttime <= monTothuSGWArray[i])
                        hour.innerHTML += "<input type='checkbox' onclick='createToast(\"" + fridaySGWArray[i] + "\");'>At :" + fridaySGWArray[i] + "</input><br/>";
                    else
                        continue;
                }
            });
        else {
            MSApp.execUnsafeLocalFunction(function () {
                hour.innerHTML = "";
                for (var i = 0; i < fridaySGWArray.length; i++)
                    hour.innerHTML += "-&nbsp;&nbsp;" + fridaySGWArray[i] + "<br/>";
            });
   
        }
    }


    if (div.className == "hidden") {
        div.className = "show";
        sch.innerHTML = "Show Map";
        sch.style.left = "3.2em";
    }
    else{
        div.className = "hidden";
        sch.innerHTML = "View Schedule";
        sch.style.left = "2.3em";
    }


}


function removeToast(date) {

    //TODO: Remove toast notification;

}

function createToast(date) {

    var hours = parseInt(date.substring(0,2), 10);
    var minutes = parseInt(date.substring(3), 10);
    var time = new Date();

    time.setHours(hours,minutes,0,0);

    var Notifications = Windows.UI.Notifications;

    var dueTime = new Date(time);

    var idNumber = Math.floor(Math.random() * 100000000);  // Generates a unique ID number for the notification.

    // Set up the notification text.
    var toastXml = Notifications.ToastNotificationManager.getTemplateContent(Notifications.ToastTemplateType.toastText02);
    var strings = toastXml.getElementsByTagName("text");
    strings[0].appendChild(toastXml.createTextNode("Your bus arrives in 10 minutes!"));
    strings[1].appendChild(toastXml.createTextNode("Message from Concordia Bus Network application."));

    // Create the toast notification object.
    var toast = new Notifications.ScheduledToastNotification(toastXml, dueTime);
    toast.id = "Toast" + idNumber;

    // Add to the schedule.
    var notifier = Notifications.ToastNotificationManager.createToastNotifier();

   notifier.addToSchedule(toast);

}


